<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Request;
use think\Db;

/**
 * 标签管理
 */
class Label extends AdminBase {

    //定义数据表
    private $db;

    public function __construct() {
        parent::__construct();
        $this->db = Db::name('Label');
    }

    //标签列表
    public function index() {
        $where = 1;
        if($label_name = input('get.label_name'))
            $where .= ' and label_name like "%'.$label_name.'%"';
        
        //获取标签列表
        $data = $this->db->where($where)->paginate(10);
        // 获取分页显示
        $page = $data->render();
        // 模板变量赋值
        $this->assign('data', $data->all());
        $this->assign('page', $page);
        return $this->fetch();
    }

    //添加标签
    public function add() {
        if (Request::instance()->isPost()) {
            $data = input('post.label_name');
            if ($data) {
                $data2 = explode(',', trim(str_replace('，', ',', $data), ','));
                foreach ($data2 as $k => $v) {
                    if (mb_strlen($v) <= 10)
                        $result[]['label_name'] = $v;
                    else
                        die(\UtilD::handleResult(0, '单个标签名称不能超过10个字'));
                }
                if ($result) {
                    $result2 = $this->db->insertAll($result);
                    
                    $result2 ? $this->success('标签添加成功') : $this->error('标签添加失败');
                }
            } else {
                $this->error('请输入标签名称');
            }
        } else {
            return $this->fetch();
        }
    }

    // 修改标签
    public function edit() {
        if (Request::instance()->isPost()) {
            $data = input('post.');
            if ($data) {
                if (mb_strlen($data['label_name']) >= 10) {
                    die(\UtilD::handleResult(0, '标签名称不能超过10个字'));
                } else {
                    $result = $this->db->update($data, ['label_id' => $data['label_id']]);
                    $result ? $this->success('标签修改成功') : $this->error('标签修改失败');
                }
            } else {
                $this->error('请输入标签名称');
            }
        } else {
            $id = Request::instance()->param('id');
            $data = $this->db->where(array('label_id' => $id))->find();
            $this->assign('data', $data);
            return $this->fetch();
        }
    }

    //删除标签
    public function delete() {
        $id = Request::instance()->param('id');
        if ($id) {
            $this->db->delete($id);
            $this->delLabelArticle($id);
            $this->success('删除成功');
        }
        $this->error('非法操作！');
    }

    //批量删除标签
    public function delAll() {
        $delid = input('post.id');
        if ($delid) {
            $this->db->delete(trim($delid, ','));
            $this->delLabelArticle($delid);
            
            die(\UtilD::handleResult(1, '删除成功,正在跳转...', array('url' => url('Label/index'))));
        }
        die(\UtilD::handleResult(0, '非法操作！'));
    }
    
    //删除中间表使用该标签的记录
    protected function delLabelArticle ($label_id) {
         if(is_numeric($label_id)){
            //删除绑定的标签
            Db::name('ArticleLabel')->where(['label_id'=>$label_id])->delete();
        }else{
            //删除绑定的标签
            Db::name('ArticleLabel')->where(['label_id'=>['in',trim($label_id, ',')]])->delete();
        }
    }
}
